﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QBluetoothDeviceDiscoveryAgent>
#include <QBluetoothDeviceInfo>
#include <QBluetoothSocket>
#include <QLowEnergyController>
#include <QLowEnergyService>
#include <QLowEnergyCharacteristic>
#include <QLowEnergyDescriptor>
#include <QBluetoothUuid>
#include <QTimer>
#include <QLabel>
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include "myTasks.h"
#include <QThread>
#include "Bluetooth.h"

#include "Logger.h"
#include "myChartView.h"
QT_CHARTS_USE_NAMESPACE

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    enum State{
        DISCONNECTED,
        CONNECTED,
        READY,
    };

    State state             = DISCONNECTED;
    QTimer *timeoutTimer    = nullptr;
    Bluetooth *bluetooth    = nullptr;
    Logger *logger          = nullptr;



    void startConnectTimeoutTimer();

public slots:
    void refreshDeviceList();
    void handleBluetoothConnect();
    void handleBluetoothDisconnect();
    void handleTransmitReady();
    void sendUserInput(char c);
    void handleAmpResults(const QList<qreal>& ampList);
private slots:
    void connectionTimeout();
    void on_RefreshDevicesButton_released();
    void on_ConnectButton_released();

    void readData();

    void on_btnDataOutput_clicked();

    void on_btnImageClear_clicked();

    void on_btnClearRev_clicked();

    void receivedDataCount();

    void on_checkBoxShowAll_clicked(bool checked);

    void on_comboBoxDataChoice_currentIndexChanged(int index);

    void on_comboBoxAxisRange_currentIndexChanged(int index);

    void on_btnStop_clicked();
public:
    //chart stuffs
    void InitChart();

    void setAmp();

private:
    QChart *chartX;
    QChart *chartY;
    QChart *chartZ;

    QLineSeries *seriesX;
    QLineSeries *seriesY;
    QLineSeries *seriesZ;

    QValueAxis *axis_tx;
    QValueAxis *axis_ty;
    QValueAxis *axis_tz;
    QValueAxis *axis_X;
    QValueAxis *axis_Y;
    QValueAxis *axis_Z;

    qreal step = 0.1;
    qreal datat = 0;
    QList<qreal> data{0, 0, 0};
    qreal maxX = 2, minX = -2;
    qreal maxY = 2, minY = -2;
    qreal maxZ = 2, minZ = -2;

    QTimer *m_timer;
    QLabel* labRevCnt;
    QLabel* labNote;
    int RevCnt = 0;
    bool recFlag = true;

    QList<qreal> accWithGList{0, 0, 0};
    QList<qreal> accNoGList{0, 0, 0};
    QList<qreal> angList{0, 0, 0};

    //记录坐标轴上的点
    QList<QList<qreal>> numVis{QList<qreal>(), QList<qreal>(), QList<qreal>()};
    QList<QMap<qreal, int>> hashMap{QMap<qreal, int>(), QMap<qreal, int>(), QMap<qreal, int>()};

signals:
    void ampOperator(const QList<qreal>&);
    //线程
private:
    QThread axisAdjustThread;
};

#endif // MAINWINDOW_H
